<template>
  <div class="customerStatistics">
    <div class="title">企微号分析<span class="icon">指标说明</span>
    </div>
    <div class="formGroup">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="130px" class="">
        <el-form-item prop="userName2">
          <div class="time-big">
            <ul class="timeType">
              <li :class="selectedRange=='today'?`active`:``" @click="getTime('today')">今天</li>
              <li :class="selectedRange=='yesterday'?`active`:``" @click="getTime('yesterday')">昨天</li>
              <li :class="selectedRange=='last7Days'?`active`:``" @click="getTime('last7Days')">近7天</li>
              <li :class="selectedRange=='last15Days'?`active`:``" @click="getTime('last15Days')">近15天</li>
              <li :class="selectedRange=='last30Days'?`active`:``" @click="getTime('last30Days')">近30天</li>
            </ul>
            <el-date-picker
              size="small"
              v-model="timeRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>

        </el-form-item>
        <el-form-item prop="userName2">
          <div class="search">
            <el-input v-model="queryParams.userName1" size="small" placeholder="搜索关键字" clearable/>
            <el-button icon="el-icon-search" size="small" @click="handleQuery"></el-button>
          </div>
        </el-form-item>
        <el-form-item prop="userName2">
          <el-select v-model="queryParams.userName2" size="small" filterable placeholder="全部分组" @change="handleQuery">
            <el-option
              v-for="item in userList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="userName2">
          <multipleWeChat :options="options" @getValue="getValue" :placeholderText="`全部企微号`"></multipleWeChat>
        </el-form-item>

      </el-form>
      <el-button  plain size="small">导出数据</el-button>
    </div>
    <div class="tableAnalysis">
      <el-table :data="tableDataList"
                ref="myBalance"
                :max-height="tableHeight"
                class="tableHightClass"
      >
        <el-table-column :label="item.label" align="center" v-for="(item,index) in tableColumns" :prop="item.key?item.key:`ziji`" :min-width="item.minWidth ?item.minWidth :``" :fixed="item.fixed?item.fixed:false" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="item.key==`create_time`">
              <div class="userInfo">
                <img src="@/assets/images/profile.jpg"/>
                <div class="user-big">
                  <div class="name">技术部</div>
                  <div class="label">群成员：3人</div>
                </div>
              </div>
            </div>
            <div v-else>{{ scope.row[item.key] }}</div>
          </template>
          <template slot="header" v-if="item.showHeaderTip">
            <div>
              {{item.label}}
              <el-tooltip effect="dark" :content="item.showHeaderTip" placement="top">
                <span><i class="el-icon-question"></i></span>
              </el-tooltip>
            </div>
          </template>
          <!--用于表格自定义-->
          <template v-if="item.childrenList && item.childrenList.length>0">
            <el-table-column :label="sitem.label" align="center" v-for="(sitem,index) in item.childrenList" :prop="sitem.key" :min-width="sitem.minWidth ?item.minWidth :``" :fixed="item.fixed?item.fixed:false" :show-overflow-tooltip="true">
              <!--用于表格自定义-->
            </el-table-column>
          </template>

        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>
<script>
import {listUser} from "@/api/system/user";
import {getTime} from "@/utils";

export default {
  name: "WeChatAnalysis",
  components: {
    'multipleWeChat': () => import('@/views/components/multipleWeChat.vue'),
  },
  data() {
    return {
      queryParams: {},
      timeRange: [],
      options: [{
        value: 1,
        label: '小红',
        WeChatNum: 2,
        WeChatName: '智行',
      }, {
        value: 2,
        label: '小军',
        WeChatNum: 1,
        WeChatName: '锐行',
      }, {
        value: 3,
        label: '小李',
        WeChatNum: 1,
        WeChatName: '锐行',
      }],
      userList: [
        {
          value: '1',
          label: '员工1'
        }, {
          value: '2',
          label: '员工2'
        }
      ],
      selectedRange: 'today',
      tableHeight:600,
      tableColumns: [
        {key: 'create_time', label: `企微号`, visible: true, minWidth: 200,},
        {
          key: '', label: '客户数据', visible: true, minWidth:100,
          childrenList: [
            {key: 'all_score', label: '总数', visible: true, minWidth: 100,},
            {key: 'all_score', label: '活跃客户', visible: true, minWidth: 100,},
            {key: 'all_score', label: '新增', visible: true, minWidth: 100,},
            {key: 'all_score', label: '新增破冰数', visible: true, minWidth: 100,},
            {key: 'all_score', label: '新增流失', visible: true, minWidth: 100,},
            {key: 'all_score', label: '累计流失', visible: true, minWidth: 100,},
          ]
        },
        {
          key: '', label: '会话数据', visible: true, minWidth: 100,
          childrenList: [
            {key: 'all_score', label: '群聊数', visible: true, minWidth: 100,},
            {key: 'all_score', label: '主动发起', visible: true, minWidth: 100,},
            {key: 'all_score', label: '消息数', visible: true, minWidth: 100,},
            {key: 'all_score', label: '客户发起', visible: true, minWidth: 100,},
            {key: 'all_score', label: '已回复', visible: true, minWidth: 100,},
            {key: 'all_score', label: '已回复占比', visible: true, minWidth: 100,},
            {key: 'all_score', label: '平均首次回复用时', visible: true, minWidth:250,},
          ]
        },
      ],
      tableDataList: [],
      total: 8, // 表格的总条数
    }
  },
  watch: {},
  computed: {
    dateRange() {
      const now = new Date();
      switch (this.selectedRange) {
        case 'yesterday':
          return [
            new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1),
            new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1),
          ];
        case 'today':
          return [
            now,
            new Date(now.getFullYear(), now.getMonth(), now.getDate()),
          ];
        case 'last7Days':
          return [
            new Date(now.getTime() - (6 * 24 * 60 * 60 * 1000)),
            now,
          ];
        case 'last15Days':
          return [
            new Date(now.getTime() - (14 * 24 * 60 * 60 * 1000)),
            now,
          ];
        case 'last30Days':
          return [
            new Date(now.getTime() - (29 * 24 * 60 * 60 * 1000)),
            now,
          ];
        default:
          return [];
      }
    },
  },
  created() {
    //this.getList();

  },
  methods: {
    getValue(obj) {
      console.log('获取行', obj);
    },
    getTime(type) {
      this.selectedRange = type;
      setTimeout(() => {
        console.log('时间范围', this.dateRange);
        this.timeRange = [];
        this.timeRange = [this.fiterTime(this.dateRange[0]), this.fiterTime(this.dateRange[1])];
        console.log('转化时间', this.timeRange);
      }, 0)
    },
    fiterTime(value) {
      if (!value) return '';
      const date = new Date(value);
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.tableDataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
}
</script>


<style scoped lang="scss">
.title {
  display: flex;
  align-items: center;
  padding: 16px 0 0;
  font-weight: bold;
  font-size: 16px;
  margin-bottom:15px;
  position: relative;

  .icon {
    font-size: 12px;
    font-weight: 400;
    color: #3070ff;
    margin-left: 10px;
  }
  .time {
    position: absolute;
    right: 0px;
    top: 10px;
    color: #8C8C8C;
    font-size: 12px;
    font-weight: 400;
  }
}

.timeType {
  display: flex;
  align-items: center;
  margin-right: 10px;

  li {
    border: 1px solid #eee;
    height: 32px;
    margin: 0;
    padding: 0 15px;
    list-style: none;
    cursor: pointer;

    &.active {
      border-color: #2d8cf0;
      color: #2d8cf0;
    }
  }
}

.time-big {
  display: flex;
  align-items: center;
}
.formGroup {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  width: 100%;
}
.dataScreening {
  display: flex;
  padding: 20px;
  background-color: #f7f7f7;
  border-radius: 4px;
  li {
    flex: 1;
    list-style: none;
    .li-title {
      margin-bottom: 4px;
      color: rgba(0, 0, 0, .45);
      font-size: 14px;
    }
    .li-number {
      color: rgba(0, 0, 0, .85);
      font-size: 24px;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
    }
  }
}
.content-center {
  display: flex;
  height: 300px;
  .center-left {
    width: 50%;
    height: 100%;
  }
  .center-right {
    width: 50%;
    height: 100%;
  }
}
.center-title {
  height: 30px;
  display: flex;
  align-items: center;
  span {
    content: "";
    width: 4px;
    height: 14px;
    background-color: #3070ff;
    display: block;
    margin-right: 5px;
  }
}
.trendAnalysis {
 height: 350px;
  width: 100%;

}
.tableAnalysis {
  padding-bottom: 20px;
  position: relative;
  .center-title {
    margin-bottom: 20px;
  }
  .exportData {
    position: absolute;
    right: 0px;
    top: 0px;

  }
}
::v-deep .el-table th {
  border-bottom: 1px solid #dfe6ec !important;
}
::v-deep .el-table--border .el-table__cell{
  border-right: 1px solid #dfe6ec !important;
}
.search {
  display: flex;

  ::v-deep.el-input__inner {
    border-right: none !important;
    border-radius: 4px 0px 0px 0px;
  }

  ::v-deep .el-button--default {
    border-radius: 0px 4px 4px 0px;
    padding: 0px 10px;
  }
}
.userInfo {
  border-radius: 4px;
  margin: 8px 0;
  cursor: pointer;
  position: relative;
  padding-left: 50px;
  font-size: 14px;
  list-style: none;
  text-align: left;
  display: flex;
  align-items: center;
  justify-items: center;

  img {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    left: 0px;
    top: 0px;
    position: absolute;
  }

  .label {
    margin-top: 2px;
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #f90;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .groupInfo {
    background-color: rgb(48, 112, 255);
    align-self: flex-start;
    width: 40px;
    padding: 2px 7px;
    display: inline-block;
    color: #fff;
    border-radius: 5px;
    margin-top: 10px;
    margin-left: 10px;

  }
  .user-big {
    display: flex;
    flex-direction: column;
    line-height: 18px;
  }
}
</style>
